package uns.fit.gis.mapdrawer;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import uns.fit.gis.Util;
import uns.fit.gis.mapobj.Segment;

public class StringDrawer {
	private Sprite alphabet;
	public static final int DELTA = 10;
	private final int INDEX_OF_SPACE = 104; // Number of characters in alphabet 105
	
	public StringDrawer(Image alphabet, Image alphabet90){
		this.alphabet = new Sprite(alphabet, 7, 14);
	}
	
	public int draw(Graphics g, byte[] bytes, int x0, int y0,
			int x1, int y1, int start) {
		
		if((x1 == x0) && (y1 == y0))
			return start;
		
		int i = start;
		
		int cx = -1, cy = -1;
		
		if (Math.abs(x1 - x0) < Math.abs(y1 - y0)) {
			int dy = y0 < y1 ? DELTA : -DELTA;
			if (cy < 0 || ((cy - y0) ^ (cy - y1)) >= 0) {
				cy = y0 + dy;
				if(start == 0) //ve tu dau chuoi ki tu
					cy += dy;
			}
			
			if(y1 > y0) {
				while((i < bytes.length) && ((cy - y0) ^ (cy - y1)) < 0) {
					int index = bytes[i] & 0xFF;
					drawCharacter(g, index, x0 + (x1 - x0) * (cy - y0) / (y1 - y0) + 3, cy + 7, Sprite.TRANS_ROT90);
					cy += dy;
					i ++;
				}
			}
			else if(y1 < y0){
				while((i < bytes.length) && ((cy - y0) ^ (cy - y1)) < 0) {
					int index = bytes[i] & 0xFF;
					drawCharacter(g, index, x0 + (x1 - x0) * (cy - y0) / (y1 - y0) + 3, cy + 7, Sprite.TRANS_ROT270);
					cy += dy;
					i ++;
				}
			}
			
		} else {
			int dx = x0 < x1 ? DELTA : -DELTA;
			if (cx < 0 || ((cx - x0) ^ (cx - x1)) >= 0) {
				cx = x0 + dx;
				if(start == 0) //ve tu dau chuoi ki tu
					cx += dx;
			}
			
			while((i < bytes.length) && ((cx - x0) ^ (cx - x1)) < 0) {
				int index = bytes[i] & 0xFF;
				drawCharacter(g, index, cx, y0 + (y1 - y0) * (cx - x0) / (x1 - x0) + 7, Sprite.TRANS_NONE);
				cx += dx;
				i ++;
			}
		}
		
		return i;
	}

	public int check(byte[] bytes, int x0, int y0,
			int x1, int y1, int start) {
		
		if((x1 == x0) && (y1 == y0))
			return start;
		
		int i = start;
		
		int cx = -1, cy = -1;
		
		if (Math.abs(x1 - x0) < Math.abs(y1 - y0)) {
			int dy = y0 < y1 ? DELTA : -DELTA;
			if (cy < 0 || ((cy - y0) ^ (cy - y1)) >= 0) {
				cy = y0 + dy;
				if(start == 0) //ve tu dau chuoi ki tu
					cy += dy;
			}
			
			if(y1 > y0) {
				while((i < bytes.length) && ((cy - y0) ^ (cy - y1)) < 0) {
					cy += dy;
					i ++;
				}
			}
			else if(y1 < y0){
				while((i < bytes.length) && ((cy - y0) ^ (cy - y1)) < 0) {
					cy += dy;
					i ++;
				}
			}
			
		} else {
			int dx = x0 < x1 ? DELTA : -DELTA;
			if (cx < 0 || ((cx - x0) ^ (cx - x1)) >= 0) {
				cx = x0 + dx;
				if(start == 0) //ve tu dau chuoi ki tu
					cx += dx;
			}
			
			while((i < bytes.length) && ((cx - x0) ^ (cx - x1)) < 0) {
				cx += dx;
				i ++;
			}
		}
		
		return i;
	}
	
	public void drawCharacter(Graphics g, int index, int x, int y, int transform) {
		try {
			if(index >= 0 && index < INDEX_OF_SPACE) {
				if(transform == Sprite.TRANS_ROT90) {
					alphabet.setFrame(index);
					this.alphabet.defineReferencePixel(3, 7);
				}
				else {
					alphabet.setFrame(index);
					this.alphabet.defineReferencePixel(3, 14);
				}
				alphabet.setTransform(transform);
				alphabet.setRefPixelPosition(x, y);
				alphabet.paint(g);
			}
		}
		catch(Exception exception) {
			exception.printStackTrace();
		}
	}
}

